/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.service;

import br.com.foxline.model.*;
import br.com.foxline.util.*;
import java.util.*;
import javax.ejb.Stateless;
import javax.persistence.*;

/**
 *
 * @author Ely
 */
@Stateless
public class CustomerService extends GenericService<Customer> {

    public CustomerService() {
        super(Customer.class);
    }

    public List<Customer> autocomplete(String query) {
        return getEntityManager().createQuery("select c from Customer c where lower(c.name) LIKE'%" + query.toLowerCase() + "%'").getResultList();
    }
  
    public void saveCustomer(Customer customer) {
        try {
            if (customer.getSpouse() != null) {
                if (customer.getSpouse().getId() == null) {
                    removeCaracter(customer);
                    customer.getSpouse().setAddresses(customer.getAddresses());
                    save(customer.getSpouse());
                    customer.setSpouse(customer.getSpouse());
                } 
            }else {
                removeCaracter(customer);
                customer.setSpouse(customer.getSpouse());
            }
            save(customer);
            Msg.messagemInfo("Cliente Cadastrado com Sucesso ");
        } catch (Exception erro) {
            erro.printStackTrace();
        }

    }

    public void updateCustomer(Customer customer) {
         try {
            if (customer.getSpouse().getId() == null) {
                removeCaracter(customer);
                customer.getSpouse().setAddresses(customer.getAddresses());
                save(customer.getSpouse());
                customer.setSpouse(customer.getSpouse());
                update(customer);
            } else {
                removeCaracter(customer);
                customer.setSpouse(customer.getSpouse());
                update(customer);
            }
            Msg.messagemInfo("Cliente Atualizado com Sucesso ");
        } catch (Exception erro) {
            erro.printStackTrace();
        }
    }

    public Customer existCustomer(Customer td) {
        Customer customer = null;
        String sql = "Select c from Customer c where lower(c.cpf) like lower(:doc)";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("doc", Caracter.removecaracter(td.getCpf()));
        try {
            customer = (Customer) query.getResultList().get(0);
        } catch (NoResultException erro) {
        } catch (NonUniqueResultException nre) {
            return null;
        } catch (Exception e) {
            return null;
        }
        return customer;
    }

    public List<Customer> findCustomer(Customer customer) {
        List<Customer> customers = new ArrayList<Customer>();

        String sql = "SELECT c FROM Customer c WHERE ";

        if ((!customer.getName().equals("")) && (customer.getName() != null)) {
            sql += " lower(c.name) like lower(:name) and  ";
        }

        if ((!customer.getCpf().equals("")) && (customer.getCpf() != null)) {
            sql += " lower(c.cpf) like lower(:cpf) and  ";
        }

        sql += " 1=1 ";

        Query query = getEntityManager().createQuery(sql);

        if ((!customer.getName().equals("")) && (customer.getName() != null)) {
            query.setParameter("name", "%" + customer.getName() + "%");
        }
        if ((!customer.getCpf().equals("")) && (customer.getCpf() != null)) {
            query.setParameter("cpf", "%" + customer.getCpf() + "%");
        }

        customers = query.getResultList();
        return customers;
    }

    public List<Address> findAddresses(Customer c) {
        String sql = "SELECT a FROM Customer c JOIN c.addresses a WHERE c.id = :id";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", c.getId());
        return (List<Address>) query.getResultList();
    }

      public List<Annex> findAnnex(Customer c) {
        String sql = "SELECT a FROM Customer c JOIN c.annexs a WHERE c.id = :id";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", c.getId());
        return (List<Annex>) query.getResultList();
    }

    public void removeCaracter(Customer customer) {
        if (customer.getCelPhone() != null) {
            customer.setCelPhone(Caracter.removecaracter(customer.getCelPhone()));
        }
        if (customer.getFixedPhone() != null) {
            customer.setFixedPhone(Caracter.removecaracter(customer.getFixedPhone()));
        }
        customer.setCpf(Caracter.removecaracter(customer.getCpf()));
        if (customer.getSpouse() != null) {
            if (customer.getSpouse().getCpf() != null) {
                customer.getSpouse().setCpf(Caracter.removecaracter(customer.getSpouse().getCpf()));
            }
            if (customer.getSpouse().getCelPhone() != null) {
                customer.getSpouse().setCelPhone(Caracter.removecaracter(customer.getSpouse().getCelPhone()));
            }
            if (customer.getSpouse().getFixedPhone() != null) {
                customer.getSpouse().setFixedPhone(Caracter.removecaracter(customer.getSpouse().getFixedPhone()));
            }
        }
    }
}
